/**
 * Created by wxb on 2019/07/03.
 */
(function () {
  'use strict';

  angular.module('BlurAdmin.pages.codeManage.flightCode')
    .controller('FlightCodeListPageCtrl', FlightCodeListPageCtrl);

  /** @ngInject */
  function FlightCodeListPageCtrl($scope, hotelManageService, toastr, $uibModal, PAGE_SIZE) {
    $scope.PAGE_SIZE = PAGE_SIZE;

    var startDate;
    var endDate;

    $scope.searchRegisterTime = function (arg) {
      var now = new Date();
      switch(arg) {
        case 0:
          $scope.chooseDisplay = '全部';
          break;
        case 1:
          $scope.chooseDisplay = '今天';
          break;
        case 3:
          $scope.chooseDisplay = '最近三天';
          break;
        case 7:
          $scope.chooseDisplay = '最近七天';
          break;
        case 30:
          $scope.chooseDisplay = '最近30天';
          break;
        default:
          $scope.chooseDisplay = '请选择..';
      }
      if (arg ===0 ) {
        startDate = undefined;
        endDate = undefined;
      }
      else if (arg === 1) {
        startDate = moment(now).startOf('day').format('YYYY-MM-DD HH:mm:ss');
        endDate = moment(now).endOf('day').format('YYYY-MM-DD HH:mm:ss');
      }
      else {
        startDate = moment(now).startOf('day').format('YYYY-MM-DD HH:mm:ss');
        endDate = moment(now).endOf('day').add(arg, 'days').format('YYYY-MM-DD HH:mm:ss');
      }
      $scope.callServer();
      $(".myRegisterTime").dropdown('toggle');
    };


    $scope.specifyRegisterRange = function (start, end) {
      $scope.chooseDisplay = start + '至' + end;
      console.log(start, end);
      startDate = moment(start).startOf('day').format('YYYY-MM-DD HH:mm:ss');
      endDate = moment(end).endOf('day').format('YYYY-MM-DD HH:mm:ss');
      $scope.callServer();
      $(".myRegisterTime").dropdown('toggle');
    };

    $scope.callServer = function (tableState, ctrl) {
      if ( !$scope.stCtrl && ctrl ) {
        $scope.stCtrl = ctrl;
      }
      if( !tableState && $scope.stCtrl ) {
        $scope.stCtrl.pipe();
        return;
      }
      console.log(tableState);

      $scope.isLoading = true;

      var params = {};
      angular.copy(tableState.search.predicateObject, params);

      if (startDate && endDate) {
        params.startDate = startDate;
        params.endDate = endDate;
      }

      params.pageNo = tableState.pagination.start/20 + 1 || 1;
      params.pageSize = tableState.pagination.number || PAGE_SIZE;
      console.log(params);
      hotelManageService.flightCode(params).then(function (data) {
        if (data && data.errcode === 0) {
          $scope.data = data.data;
          tableState.pagination.numberOfPages = data.data.totalPage;
          $scope.isLoading = false;
        }
      });
    };


  }
})();
